function val = insideTriangle(A,B,C,P)
%INSIDETRIANGLE (A,B,C,P) 2D points for triangle A,B,C and the point P
%   return true if the point P is inside the triangle A,B,C
%   used the code from:
%   http://www.blackpawn.com/texts/pointinpoly/default.html


% Compute vectors        
v0 = C - A;
v1 = B - A;
v2 = P - A;

% Compute dot products
dot00 = v0'*v0;
dot01 = v0'*v1;
dot02 = v0'*v2;
dot11 = v1'*v1;
dot12 = v1'*v2;

% Compute barycentric coordinates
invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
u = (dot11 * dot02 - dot01 * dot12) * invDenom;
v = (dot00 * dot12 - dot01 * dot02) * invDenom;

% Check if point is in triangle
val= (u > 0) && (v > 0) && (u + v < 1);

end
